package com.baimo.support.api;


import com.baimo.support.dao.model.UserReviewModel;
import com.baimo.support.dao.model.UserTempModel;
import com.baimo.support.service.IUserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import static com.baimo.support.commons.constants.Constants.CITY_CODE;
import static com.baimo.support.commons.constants.Constants.CODE_CITY;

/**
 * 初始化启动信息
 * Created by wang on 2019/11/20.
 */
@Component
@Order(1)
public class ApplicationInit implements ApplicationRunner {
    @Autowired
    IUserService iUserService;
    private final String cityCodeFile = "citycode.xlsx";
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //获取本地城市编码的excel
        File cityFile = new File(ResourceUtils.getURL("classpath:").getPath() + File.separator + cityCodeFile);
        if(!cityFile.exists()) return;
        //解析城市编码的excel
        long startTime = System.currentTimeMillis();
        readCityExcelFile(new FileInputStream(cityFile),cityCodeFile);
        long endTime = System.currentTimeMillis();
        System.out.println("总共耗时:" + (endTime - startTime));
//        List<UserTempModel> results = iUserService.findTempUser();
//        System.out.println("Temp USer Results总数:" + results.size());

        List<UserReviewModel> reviews = iUserService.findAllUserReviews();
        System.out.println("User Reviews Count总数:" + reviews.size());

    }
    /**
     * 解析一下本地excel
     * @param inputStream
     * @param fileName
     */
    public void readCityExcelFile(InputStream inputStream, String fileName){

        /**
         * 这个inputStream文件可以来源于本地文件的流，
         *  也可以来源与上传上来的文件的流，也就是MultipartFile的流，
         *  使用getInputStream()方法进行获取。
         */

        /**
         * 然后再读取文件的时候，应该excel文件的后缀名在不同的版本中对应的解析类不一样
         * 要对fileName进行后缀的解析
         */
        Workbook workbook = null;

        try {
            //判断什么类型文件
            if (fileName.endsWith(".xls")) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (fileName.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(inputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (workbook == null) {
            return ;
        } else {
            //获取所有的工作表的的数量
            int numOfSheet = workbook.getNumberOfSheets();
            //遍历表
            for (int i = 0; i < numOfSheet; i++) {
                //获取一个sheet也就是一个工作本。
                Sheet sheet = workbook.getSheetAt(i);
                if(sheet == null) continue;
                //获取一个sheet有多少Row
                int lastRowNum = sheet.getLastRowNum();
                if(lastRowNum == 0) continue;
                Row row ;
                for (int j  = 1; j <= lastRowNum; j++) {
                    row = sheet.getRow(j);
                    if(row == null) {
                        continue;
                    }
                    //获取一个Row有多少Cell
                    short lastCellNum = row.getLastCellNum();
                    for (int k = 0; k <= lastCellNum; k++) {
                        if(row.getCell(k)==null) {
                            continue;
                        }
                        String res = row.getCell(k).getStringCellValue().trim();
                        //根据空格分割城市与编码的内容
                        String [] arr = res.split("\\s+");
                        if(arr.length > 1){
                            CITY_CODE.put(arr[0],arr[1]);
                            CODE_CITY.put(arr[1],arr[0]);
                        }
                    }
                }
            }
        }
    }
}
